// pages/index_lib/index_lib.js
const baseURL = 'http://100.2.178.56:8080'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        stuId: '',
        stuPass: '',
        count: 0,
        check_login: true,
        sessionId: null,

    },
    async login_click() {
        var that = this;
        var that = this;



        //获取UserInfo
        wx.getUserProfile({
            desc: '用于登录信息比对',
            fail: console.log,
            success: (res2) => {

                console.log(res2)

                wx.getSetting({
                    withSubscriptions: true,
                    success: console.log
                })

                //后台解析加密信息，检查用户是否注册信息
                wx.request({
                    url: baseURL + '/user/authLogin',
                    data: {
                        "sessionId": that.data.sessionId,
                        "encryptedData": res2.encryptedData,
                        "iv": res2.iv
                    },
                    success: function (res3) {

                        //未注册，跳转到注册页面
                        //209———用户未审核
                        if (res3.data.code == 209) {
                            wx.showToast({
                                title: res3.data.message,
                                duration: 2000
                            })
                            //208——用户未注册
                        } else if (res3.data.code == 208) {
                            wx.showToast({
                                title: res3.data.message,
                                icon: 'error',
                                duration: 2000
                            })
                            wx.navigateTo({
                                url: '/pages/regist_judge/regist_judge',
                            })
                            //用户登录成功
                        } else if (res3.data.code == 0) {
                            wx.hideLoading()
                            console.log(res3.data.data.token);
                            /**
                             * 1、根据返回的是student还是teacher，跳转到对应的页面
                             */
                            var app = getApp();
                            app.globalStuUseId = res3.data.data.stuUseId
                            if (res3.data.data.teacher == null) {
                                /**
                                 * 封装全局变量
                                 */

                                app.globalUserInfo = res3.data.data.student;
                                app.globalGrade = res3.data.data.grade;
                                app.globalAcademy = res3.data.data.academy;
                                app.user_reserve = res3.data.data.reserve;
                                app.honest_grade = res3.data.data.honestGrade;
                                console.log("app.globalUserInfo:" + res3.data.data.student)

                            } else {

                                app.globalUserInfo_tea = res3.data.data.teacher;
                                console.log(app.globalUserInfo_tea)
                                /**
                                 * 封装全局变量
                                 */
                            }
                            app.token = res3.data.data.token;
                            setTimeout(function () {
                                wx.switchTab({
                                    url: '/pages/main_lib/main_lib',
                                })
                            }, 1500)
                            //其他错误信息 
                        } else {
                            wx.showToast({
                                title: res3.data.message,
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
            }

        })

        wx.requestSubscribeMessage({
            tmplIds: ['O_DfI4X3KuxpQUC3IVndbFyJ34oxISBq6KPS4Iq9ibk', 'O_DfI4X3KuxpQUC3IVndbGWx8hJlXFXGXvzuVnpKbRw'],
        })
        await wx.login({}).then((res) => {
            wx.showLoading({
                title: '登录中。。。',
            })
            console.log(res)
            // 获取sessionId
            wx.request({
                url: baseURL + '/user/getSessionId',
                data: {
                    "code": res.code
                },
                success: function (res1) {
                    that.setData({
                        sessionId: res1.data.data.sessionId
                    })
                }
            })
        })

    },
    regist_click: function () {
        wx.navigateTo({
            url: '/pages/regist_lib/regist_lib',
        })
    },


    input_stuId: function (e) {
        this.setData({
            stuId: e.detail.value
        })
    },
    input_stuPass: function (e) {
        this.setData({
            stuPass: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {



    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})